﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class DBAccess
    {
        static string conStr = @"Data Source=SunHALO-PC;Initial Catalog=BanPM;Integrated Security=True";
        static SqlConnection con = new SqlConnection(conStr);
        private SqlConnection conn;
        private SqlCommand cmnd;
        private SqlDataAdapter da;
        private SqlDataReader dr;
        public DBAccess()
        {
            conn = new SqlConnection(conStr);
            cmnd = con.CreateCommand();
        }
        public DataTable GetDataTable(string query, CommandType ct, params SqlParameter[] param)
        {
            cmnd.Parameters.Clear();
            cmnd.CommandText = query;
            cmnd.CommandType = ct;
            foreach (SqlParameter p in param)
            {
                cmnd.Parameters.Add(p);
            }
            da = new SqlDataAdapter(cmnd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        public DataSet GetDataSet(string query, CommandType ct, params SqlParameter[] param)
        {
            cmnd.Parameters.Clear();
            cmnd.CommandText = query;
            cmnd.CommandType = ct;
            foreach (SqlParameter p in param)
            {
                cmnd.Parameters.Add(p);
            }
            da = new SqlDataAdapter(cmnd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
        public bool DoExecuteReader(string query, CommandType ct, params SqlParameter[] param)
        {
            bool res = false;
            cmnd.Parameters.Clear();
            cmnd.CommandText = query;
            cmnd.CommandType = ct;
            foreach (SqlParameter p in param)
            {
                cmnd.Parameters.Add(p);
            }
            con.Open();
            SqlDataReader dr;
            dr = cmnd.ExecuteReader();
            if (dr.HasRows)
                res = true;
            con.Close();
            return res;
        }
        public bool DoExcecute(string query, CommandType ct, params SqlParameter[] param)
        {
            bool res = false;
            cmnd.Parameters.Clear();
            cmnd.CommandText = query;
            cmnd.CommandType = ct;
            foreach (SqlParameter p in param)
            {
                cmnd.Parameters.Add(p);
            }
            con.Open();
            int n = cmnd.ExecuteNonQuery();
            con.Close();
            if (n == 1)
                res = true;
            return res;
        }
    }
}
